#include "CRoleConfigMgr.h"
#include "../mainctrl.h"
#include "commclass.h"
#include <iostream>
#include <fstream>

#include "pb_cfg_role.pb.h"

int CRoleConfigMgr::LoadConfig(const char* sConfigFile)
{
    int iRet = 0;

    if (sConfigFile == NULL)
    {
        CT_ERROR(("sConfigFile is null"));
        return RET_SYSTEM_FAIL;
    }

    pb_cfg_role stRoleCfg;
    std::fstream stFileInput(sConfigFile, std::ios::in | std::ios::binary);
    if (!stRoleCfg.ParseFromIstream(&stFileInput))
    {
        CT_ERROR(("Failed to Parse stRoleCfg!"));
        return RET_SYSTEM_FAIL;
    }

    for (int i = 0; i < stRoleCfg.level_config_rows_size(); ++i)
    {
        const pb_cfg_role_pb_cfg_level_config_unit & stRoleConfigPbCfgUnit 
            = stRoleCfg.level_config_rows(i);

        iRet = _stLevelUpConfig.AddLevelUpExp((unsigned char)(stRoleConfigPbCfgUnit.level() & 0xFF), (unsigned int)stRoleConfigPbCfgUnit.level_up_exp());
        CT_RETURN(iRet);

        //炼金配置
        iRet = _stAlchemyConfig.AddConfig((unsigned char)(stRoleConfigPbCfgUnit.level() & 0xFF), (unsigned int)stRoleConfigPbCfgUnit.alchemy_get_silver());
        CT_RETURN(iRet);
    }

	CRoleInitCfgData stInitCfgData;
    for (int i = 0; i < stRoleCfg.role_init_rows_size(); ++i)
    {
        const pb_cfg_role_pb_cfg_role_init_unit & stRoleInitCfg 
            = stRoleCfg.role_init_rows(i);

		stInitCfgData.Clear();

		stInitCfgData._uiID = stRoleInitCfg.id();
		stInitCfgData._iInt = stRoleInitCfg.wit();
		stInitCfgData._iVit = stRoleInitCfg.vit();
		stInitCfgData._iStr = stRoleInitCfg.str();
		stInitCfgData._iAgi = stRoleInitCfg.agi();
        stInitCfgData._usHelmetId = (unsigned short)(stRoleInitCfg.helmet_id() & 0x7FFF);
        stInitCfgData._usColthId = (unsigned short)(stRoleInitCfg.cloth_id() & 0x7FFF);
        stInitCfgData._usWeaponId = (unsigned short)(stRoleInitCfg.weapon_id() & 0x7FFF);
        stInitCfgData._usShoeId = (unsigned short)(stRoleInitCfg.shoe_id() & 0x7FFF);
        stInitCfgData._iStatusGroupID = stRoleInitCfg.status_group();
		stInitCfgData._uiNormalSkill = (unsigned int)stRoleInitCfg.normal_skill();
		stInitCfgData._uiAcadSkill = (unsigned int)stRoleInitCfg.acad_skill();
		stInitCfgData._uiMagicSkill = (unsigned int)stRoleInitCfg.magic_skill();

        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_HP] = stRoleInitCfg.hp();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_PHY_ATK] = stRoleInitCfg.phy_atk();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_PHY_DEF] = stRoleInitCfg.phy_def();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_MAG_ATK] = stRoleInitCfg.mag_atk();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_MAG_DEF] = stRoleInitCfg.mag_def();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_HIT] = stRoleInitCfg.hit();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_DODGE] = stRoleInitCfg.dodge();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_DOUBLE] = stRoleInitCfg.role_double();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_ANTI_DOUBLE] = stRoleInitCfg.anti_double();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_BLOCK] = stRoleInitCfg.block();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_ANTI_BLOCK] = stRoleInitCfg.anti_block();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_TURN_ORDRE] = stRoleInitCfg.order();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_CRIT] = stRoleInitCfg.crit();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_IMMUNE_INJURY] = stRoleInitCfg.immune_injury();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_REACT_INJURY] = stRoleInitCfg.react_injury();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_PENETRATE] = stRoleInitCfg.penetrate();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_TREATMENT] = stRoleInitCfg.treatment();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_FIGHT_BACK] = stRoleInitCfg.fight_back();
        stInitCfgData._stInitAttr._aiAttr[EXT_ATTR_ACT_ANGER] = stRoleInitCfg.act_anger();

        iRet = CMainCtrl::m_pstMain->m_stRoleMgr.CheckValidCareer(
            (int)stInitCfgData._uiID);
        CT_RETURN(iRet);

        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.IsValidSkill(
            stInitCfgData._uiNormalSkill, CSkill::SKILL_TYPE_NORMAL);
        CT_RETURN(iRet);

        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.IsValidSkill(
            stInitCfgData._uiAcadSkill, CSkill::SKILL_TYPE_ACADEMIC);
        CT_RETURN(iRet);

        iRet = CMainCtrl::m_pstMain->m_stSkillMgr.IsValidSkill(
            stInitCfgData._uiMagicSkill, CSkill::SKILL_TYPE_MAGIC);
        CT_RETURN(iRet);

        iRet = CMainCtrl::m_pstMain->m_stStatusMgr.IsValidStatusGroup(
            stInitCfgData._iStatusGroupID);
        CT_RETURN(iRet);

		stInitCfgData._bHasCfg = true;

        iRet = AddRoleInitCfg(stInitCfgData);
        CT_RETURN(iRet);
    }

	CRoleAttrCfgData stAttrCfgData;
    for (int i = 0; i < stRoleCfg.role_attr_conf_rows_size(); ++i)
    {
        const pb_cfg_role_pb_cfg_role_attr_conf_unit & stRoleAttrCfg 
            = stRoleCfg.role_attr_conf_rows(i);

		stAttrCfgData.Clear();

        stAttrCfgData.SetID(stRoleAttrCfg.id());
        stAttrCfgData.SetLevelUnit(stRoleAttrCfg.level_unit());
        stAttrCfgData.SetHP(stRoleAttrCfg.hp());
        stAttrCfgData.SetPhyAtk(stRoleAttrCfg.phy_atk());
        stAttrCfgData.SetPhyDef(stRoleAttrCfg.phy_def());
        stAttrCfgData.SetMagAtk(stRoleAttrCfg.mag_atk());
        stAttrCfgData.SetMagDef(stRoleAttrCfg.mag_def());
        stAttrCfgData.SetHit(stRoleAttrCfg.hit());
        stAttrCfgData.SetDodge(stRoleAttrCfg.dodge());
        stAttrCfgData.SetDouble(stRoleAttrCfg.role_double());
        stAttrCfgData.SetAntiDouble(stRoleAttrCfg.anti_double());
        stAttrCfgData.SetBlock(stRoleAttrCfg.block());
        stAttrCfgData.SetAntiBlock(stRoleAttrCfg.anti_block());
        stAttrCfgData.SetOrder(stRoleAttrCfg.order());
        stAttrCfgData.SetCrit(stRoleAttrCfg.crit());
        stAttrCfgData.SetImmuneInjury(stRoleAttrCfg.immune_injury());
        stAttrCfgData.SetReactInjury(stRoleAttrCfg.react_injury());
        stAttrCfgData.SetPenetrate(stRoleAttrCfg.penetrate());
        stAttrCfgData.SetTreatment(stRoleAttrCfg.treatment());
        stAttrCfgData.SetFightBack(stRoleAttrCfg.fight_back());
        stAttrCfgData.SetActAnger(stRoleAttrCfg.act_anger());

        iRet = _stRoleAttrCfg.AddConfig(stAttrCfgData);
        CT_RETURN(iRet);
    }

    return 0;
}

